function dancilingdu(value) {

    $(".subscription-list-item").removeClass("active")
    $(value).addClass("active");

    let subjectId = $('#tempSubjectId').val();

    getAjax('/stu/subject/subscribe/unit?subjectId=' + subjectId, function (res) {
        if (res.success) {
            let data = res.data;
            $('#unit').empty();
            for (let i = 0; i < data.length; i++) {
                let e = data[i];
                var tag = '';
                tag = '<div onclick="lingdu(this)" subjectName="' + e.name + '" subjectId="' + e.id + '" class="subscription-card subscription-success-lingdu align-center">';
                tag += '<img class="icon" src="../../assets/images/icon/subscription-icon.png" alt="">';
                tag += '<span class="line-clamp-2 f18 color_f">' + e.name + '</span></div>';
                $('#unit').append(tag);
            }
        }
    })
}


function lingdu(value) {

    $('#lingdu').show();
    $('#lingdu-subject-name').html($(value).attr('subjectName'));
    let subjectId = $(value).attr('subjectId');


    getAjax('/stu/vocabulary/unit?subjectId=' + subjectId, function (res) {
        if (res.success) {
            $('#lingdu-data').empty();
            var bookData = res.data;
            setLocalStorage('lingduData', JSON.stringify(bookData));
            for (let i = 0; i < bookData.length; i++) {
                let d = bookData[i];
                var tag = '<div class="vocabulary-card-wrap">';
                tag += '<div class="vocabulary-card vocabulary-card-reverse f20">' + d.explain + '</div>'
                tag += '<div class="vocabulary-card2">'
                tag += '<div class="label f30">' + d.en + '</div>'
                tag += '<div class="tips2 f20">[' + d.usPhonetic + ']</div>'
                tag += '<div class="tips2 f20">[' + d.explain + ']</div>'
                tag += '<div class="tips2 f20">快读四遍说中文</div>'
                tag += '<div onclick="playPhonetic(this)" speech="' + d.usSpeech + '" class="iconfont lltingyuanyin f20"></div>'
                tag += '</div></div>'
                $('#lingdu-data').append(tag);
            }
        }
    })


}

function lingduConfig() {
    layer.open({
        type: 2,
        area: ['500px', '500px'], // 宽高
        content: '/views/home/lingdu-config.html'
    });
}

var status = 1;

var audio = new Audio();


var typeGlobal;

async function read(type) {

    status = 1;

    typeGlobal = type;

    $('#read4').html('全部暂停');
    let vArr = getListLocalStorage('lingduData');
    setLocalStorage("playArr", JSON.stringify([]))


    let vit = getLocalStorage("vit");
    let sit = getLocalStorage("sit");
    let threeIntervalTime = getLocalStorage("threeIntervalTime");
    let nineIntervalTime = getLocalStorage("nineIntervalTime");
    let speed = getLocalStorage("speed");

    let playArr = vArr;
    setLocalStorage("playArr", JSON.stringify(playArr))

    await readContinue(type);

}


async function readContinue(type) {





    let vit = getLocalStorage("vit");
    let sit = getLocalStorage("sit");
    let threeIntervalTime = getLocalStorage("threeIntervalTime");
    let nineIntervalTime = getLocalStorage("nineIntervalTime");
    let speed = getLocalStorage("speed");
    var index = 1;


    while (getListLocalStorage('playArr').length > 0) {

        if (type == 1) {
            let vArr = getListLocalStorage('playArr');
            let shift = vArr.shift();
            setLocalStorage('playArr', JSON.stringify(vArr))
            createAudio(shift);
            await sleepFun(parseInt(sit) * 1000 + 800);
        }

        if (type == 2) {
            let vArr = getListLocalStorage('playArr');
            let shift = vArr.shift();
            setLocalStorage('playArr', JSON.stringify(vArr))
            createAudio(shift);
            console.log("index:{}", index);
            if (index % 3 === 0) {
                console.log('整除');
                await sleepFun(parseInt(threeIntervalTime) * 1000 + 1000);
            } else {
                await sleepFun(parseInt(sit) * 1000 + 800);
            }
        }

        if (type == 3) {
            let vArr = getListLocalStorage('playArr');
            let shift = vArr.shift();
            setLocalStorage('playArr', JSON.stringify(vArr))
            createAudio(shift);
            console.log("index:{}", index);
            if (index % 9 === 0) {
                console.log('整除');
                await sleepFun(parseInt(nineIntervalTime) * 1000 + 1000);
            } else {
                await sleepFun(parseInt(sit) * 1000 + 800);
            }
        }
        index++;
    }
}

function createAudio(v) {
    let speed = getLocalStorage("speed");
    audio.src = v.usSpeech;
    if (speed == 2) {
        audio.playbackRate = 0.5
    } else if (speed == 3) {
        audio.playbackRate = 1.5
    }
    audio.play();
}


function subGroup(arr, len) {
    var newArr = [];
    for (var i = 0; i < arr.length; i += len) {
        newArr.push(arr.slice(i, i + len));
    }
    return newArr;
}

function sleepFun(s) {
    return new Promise((resolve) => {
        setTimeout(resolve, s);
    })
}

function playOrPause() {
    if (status == 1) {
        status = 0;
        $('#read4').html('全部播放');

        let vArr = getListLocalStorage('playArr');
        setLocalStorage('play-zt', JSON.stringify(vArr))

        setLocalStorage('playArr', JSON.stringify([]))

    } else {
        status = 1;
        $('#read4').html('全部暂停');

        let playZt = getListLocalStorage('play-zt');
        setLocalStorage('playArr', JSON.stringify(playZt))

        readContinue(typeGlobal)
    }
}